{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import struct\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import talib as tdx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "def readTdxLdayFile(fname=\"data/sh000001.day\"):\n",
    "  dataSet=[]\n",
    "  with open(fname,'rb') as fl:\n",
    "    buffer=fl.read() #读取数据到缓存\n",
    "    size=len(buffer) \n",
    "    rowSize=32 #通信达day数据，每32个字节一组数据\n",
    "    code=os.path.basename(fname).replace('.day','')\n",
    "    for i in range(0,size,rowSize): #步长为32遍历buffer\n",
    "      row=list( struct.unpack('IIIIIfII',buffer[i:i+rowSize]) )\n",
    "      row[1]=row[1]/100\n",
    "      row[2]=row[2]/100\n",
    "      row[3]=row[3]/100\n",
    "      row[4]=row[4]/100\n",
    "      row.pop() #移除最后无意义字段\n",
    "      row.insert(0,code)\n",
    "      dataSet.append(row) \n",
    "\n",
    "  data=pd.DataFrame(data=dataSet,columns=['code','tradeDate','open','high','low','close','amount','vol'])\n",
    "  data=data.set_index(['tradeDate'])\n",
    "  return code, data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "def select1(code, data):\n",
    "    # 连续三日缩量\n",
    "    cn = data.close.iloc[-1]\n",
    "#     df=pd.concat([tdx.MA(data.close, x) for x in (5,10,20,30,60,90,120,250,500,750,1000,1500,2000,2500,) ], axis = 1).dropna()[-1:]\n",
    "    df=pd.concat([tdx.MA(data.close, x) for x in (5,10,20,30,60,90,120,250,500,750,1000,1500,2000,2500,) ], axis = 1)[-1:]\n",
    "    df.columns = [u'm5',u'm10',u'm20',u'm30',u'm60',u'm90',u'm120', u'm250', u'm500', u'm750', u'm1000', u'm1500', u'm2000', u'm2500']  \n",
    "    df_c2 = df.m5 > df.m10\n",
    "    df_c1 = cn > df.m5\n",
    "    df_c = cn > df.m5\n",
    "    df_h = df.apply(lambda x:cn > x.max() ,  axis = 1 )\n",
    "#     df_l = df.apply(lambda x:x.min() >= cl,  axis = 1 )\n",
    "    \n",
    "    df['dfh'] = df_h\n",
    "    df['dfc2'] = df_c2\n",
    "    df['dfc1'] = df_c1\n",
    "    df['code'] =code\n",
    "#     out=df.iloc[-1].apply(lambda x: True if x>cl and x < ch else False)\n",
    "    df=df.reset_index('tradeDate')\n",
    "    df=df.set_index(['code','tradeDate'])\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [],
   "source": [
    "from threading import Thread, current_thread, Lock\n",
    "import multiprocessing #import Pool, cpu_count, Queue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [],
   "source": [
    "def asyncCalc(fname, queue):\n",
    "  code, df = readTdxLdayFile(fname)\n",
    "  queue.put(select1(code, df))\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": [
    "def readPath(path):\n",
    "  files = os.listdir(path)\n",
    "  # codes=[]\n",
    "  q = multiprocessing.Queue()\n",
    "  jobs = []\n",
    "  # dataSet=[]multiprocessing\n",
    "  pool_size = multiprocessing.cpu_count()\n",
    "  pool = multiprocessing.Pool(pool_size)\n",
    "  output=pd.DataFrame()\n",
    "  for i in range(0,len(files)):\n",
    "    fname = os.path.join(path,files[i])\n",
    "    if os.path.isdir(fname):\n",
    "      continue\n",
    "    pool.apply_async(asyncCalc, args=(fname))\n",
    "    p = multiprocessing.Process(target=asyncCalc, args=(fname, q))\n",
    "    jobs.append(p)\n",
    "    p.start()\n",
    "  \n",
    "  for p in jobs:\n",
    "    p.join()\n",
    "\n",
    "  for j in jobs:\n",
    "    t = q.get()\n",
    "    if t is not None:\n",
    "      output=output.append(t)\n",
    "  return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [],
   "source": [
    "output=readPath('/tmp/easyquant/tdx/data') #读取目录下面的所有文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>m5</th>\n",
       "      <th>m10</th>\n",
       "      <th>m20</th>\n",
       "      <th>m30</th>\n",
       "      <th>m60</th>\n",
       "      <th>m90</th>\n",
       "      <th>m120</th>\n",
       "      <th>m250</th>\n",
       "      <th>m500</th>\n",
       "      <th>m750</th>\n",
       "      <th>m1000</th>\n",
       "      <th>m1500</th>\n",
       "      <th>m2000</th>\n",
       "      <th>m2500</th>\n",
       "      <th>dfh</th>\n",
       "      <th>dfc2</th>\n",
       "      <th>dfc1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>code</th>\n",
       "      <th>tradeDate</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>sh000001</th>\n",
       "      <th>20190618</th>\n",
       "      <td>2895.974</td>\n",
       "      <td>2880.922</td>\n",
       "      <td>2886.1995</td>\n",
       "      <td>2892.297</td>\n",
       "      <td>3020.283833</td>\n",
       "      <td>2973.75</td>\n",
       "      <td>2867.055833</td>\n",
       "      <td>2796.51672</td>\n",
       "      <td>3036.69224</td>\n",
       "      <td>3058.255453</td>\n",
       "      <td>3159.53547</td>\n",
       "      <td>2920.746953</td>\n",
       "      <td>2785.27774</td>\n",
       "      <td>2800.640624</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sh000003</th>\n",
       "      <th>20190618</th>\n",
       "      <td>2895.974</td>\n",
       "      <td>2880.922</td>\n",
       "      <td>2886.1995</td>\n",
       "      <td>2892.297</td>\n",
       "      <td>3020.283833</td>\n",
       "      <td>2973.75</td>\n",
       "      <td>2867.055833</td>\n",
       "      <td>2796.51672</td>\n",
       "      <td>3036.69224</td>\n",
       "      <td>3058.255453</td>\n",
       "      <td>3159.53547</td>\n",
       "      <td>2920.746953</td>\n",
       "      <td>2785.27774</td>\n",
       "      <td>2800.640624</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sh000004</th>\n",
       "      <th>20190618</th>\n",
       "      <td>2895.974</td>\n",
       "      <td>2880.922</td>\n",
       "      <td>2886.1995</td>\n",
       "      <td>2892.297</td>\n",
       "      <td>3020.283833</td>\n",
       "      <td>2973.75</td>\n",
       "      <td>2867.055833</td>\n",
       "      <td>2796.51672</td>\n",
       "      <td>3036.69224</td>\n",
       "      <td>3058.255453</td>\n",
       "      <td>3159.53547</td>\n",
       "      <td>2920.746953</td>\n",
       "      <td>2785.27774</td>\n",
       "      <td>2800.640624</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sh000002</th>\n",
       "      <th>20190618</th>\n",
       "      <td>2895.974</td>\n",
       "      <td>2880.922</td>\n",
       "      <td>2886.1995</td>\n",
       "      <td>2892.297</td>\n",
       "      <td>3020.283833</td>\n",
       "      <td>2973.75</td>\n",
       "      <td>2867.055833</td>\n",
       "      <td>2796.51672</td>\n",
       "      <td>3036.69224</td>\n",
       "      <td>3058.255453</td>\n",
       "      <td>3159.53547</td>\n",
       "      <td>2920.746953</td>\n",
       "      <td>2785.27774</td>\n",
       "      <td>2800.640624</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          m5       m10        m20       m30          m60  \\\n",
       "code     tradeDate                                                         \n",
       "sh000001 20190618   2895.974  2880.922  2886.1995  2892.297  3020.283833   \n",
       "sh000003 20190618   2895.974  2880.922  2886.1995  2892.297  3020.283833   \n",
       "sh000004 20190618   2895.974  2880.922  2886.1995  2892.297  3020.283833   \n",
       "sh000002 20190618   2895.974  2880.922  2886.1995  2892.297  3020.283833   \n",
       "\n",
       "                        m90         m120        m250        m500         m750  \\\n",
       "code     tradeDate                                                              \n",
       "sh000001 20190618   2973.75  2867.055833  2796.51672  3036.69224  3058.255453   \n",
       "sh000003 20190618   2973.75  2867.055833  2796.51672  3036.69224  3058.255453   \n",
       "sh000004 20190618   2973.75  2867.055833  2796.51672  3036.69224  3058.255453   \n",
       "sh000002 20190618   2973.75  2867.055833  2796.51672  3036.69224  3058.255453   \n",
       "\n",
       "                         m1000        m1500       m2000        m2500    dfh  \\\n",
       "code     tradeDate                                                            \n",
       "sh000001 20190618   3159.53547  2920.746953  2785.27774  2800.640624  False   \n",
       "sh000003 20190618   3159.53547  2920.746953  2785.27774  2800.640624  False   \n",
       "sh000004 20190618   3159.53547  2920.746953  2785.27774  2800.640624  False   \n",
       "sh000002 20190618   3159.53547  2920.746953  2785.27774  2800.640624  False   \n",
       "\n",
       "                    dfc2   dfc1  \n",
       "code     tradeDate               \n",
       "sh000001 20190618   True  False  \n",
       "sh000003 20190618   True  False  \n",
       "sh000004 20190618   True  False  \n",
       "sh000002 20190618   True  False  "
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [],
   "source": [
    "code, data = readTdxLdayFile('/tmp/easyquant/tdx/data/sh000001.day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>m5</th>\n",
       "      <th>m10</th>\n",
       "      <th>m20</th>\n",
       "      <th>m30</th>\n",
       "      <th>m60</th>\n",
       "      <th>m90</th>\n",
       "      <th>m120</th>\n",
       "      <th>m250</th>\n",
       "      <th>m500</th>\n",
       "      <th>m750</th>\n",
       "      <th>m1000</th>\n",
       "      <th>m1500</th>\n",
       "      <th>m2000</th>\n",
       "      <th>m2500</th>\n",
       "      <th>dfh</th>\n",
       "      <th>dfc2</th>\n",
       "      <th>dfc1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>code</th>\n",
       "      <th>tradeDate</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>sh000001</th>\n",
       "      <th>20190618</th>\n",
       "      <td>2895.974</td>\n",
       "      <td>2880.922</td>\n",
       "      <td>2886.1995</td>\n",
       "      <td>2892.297</td>\n",
       "      <td>3020.283833</td>\n",
       "      <td>2973.75</td>\n",
       "      <td>2867.055833</td>\n",
       "      <td>2796.51672</td>\n",
       "      <td>3036.69224</td>\n",
       "      <td>3058.255453</td>\n",
       "      <td>3159.53547</td>\n",
       "      <td>2920.746953</td>\n",
       "      <td>2785.27774</td>\n",
       "      <td>2800.640624</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          m5       m10        m20       m30          m60  \\\n",
       "code     tradeDate                                                         \n",
       "sh000001 20190618   2895.974  2880.922  2886.1995  2892.297  3020.283833   \n",
       "\n",
       "                        m90         m120        m250        m500         m750  \\\n",
       "code     tradeDate                                                              \n",
       "sh000001 20190618   2973.75  2867.055833  2796.51672  3036.69224  3058.255453   \n",
       "\n",
       "                         m1000        m1500       m2000        m2500    dfh  \\\n",
       "code     tradeDate                                                            \n",
       "sh000001 20190618   3159.53547  2920.746953  2785.27774  2800.640624  False   \n",
       "\n",
       "                    dfc2   dfc1  \n",
       "code     tradeDate               \n",
       "sh000001 20190618   True  False  "
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "select1(code,data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cn = data.close.iloc[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cn=cn+1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.concat([tdx.MA(data.close, x) for x in (5,10,20,30,60,90,120,250,500,750,1000,21500,20000,25000,) ], axis = 1)[-1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6965</th>\n",
       "      <td>2895.974</td>\n",
       "      <td>2880.922</td>\n",
       "      <td>2886.1995</td>\n",
       "      <td>2892.297</td>\n",
       "      <td>3020.283833</td>\n",
       "      <td>2973.75</td>\n",
       "      <td>2867.055833</td>\n",
       "      <td>2796.51672</td>\n",
       "      <td>3036.69224</td>\n",
       "      <td>3058.255453</td>\n",
       "      <td>3159.53547</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            0         1          2         3            4        5   \\\n",
       "6965  2895.974  2880.922  2886.1995  2892.297  3020.283833  2973.75   \n",
       "\n",
       "               6           7           8            9           10  11  12  13  \n",
       "6965  2867.055833  2796.51672  3036.69224  3058.255453  3159.53547 NaN NaN NaN  "
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.columns = [u'm5',u'm10',u'm20',u'm30',u'm60',u'm90',u'm120', u'm250', u'm500', u'm750', u'm1000', u'm1500', u'm2000', u'm2500']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_c = df.m5 > df.m10\n",
    "df_c1 = cn > df.m5\n",
    "df_h = df.apply(lambda x:cn > x.max() ,  axis = 1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "da=data_df.reset_index('tradeDate')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_c1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.to_datetime(da.tradeDate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data_df.to_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_df.index[,-1:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def select1(code,data):\n",
    "    # 连续三日缩量\n",
    "    ch= data.close.iloc[-1] * 1.1\n",
    "    cl= data.close.iloc[-1] * 0.9\n",
    "#     ch= data.close * 1.1\n",
    "#     cl = data.close * 0.9\n",
    "    \n",
    "    df=pd.concat([tdx.MA(data.close, x) for x in (5,10,20,30,60,90,120,250) ], axis = 1).dropna()[-1:]\n",
    "    df.columns = [u'm5',u'm10',u'm20',u'm30',u'm60',u'm90',u'm120', u'm250']  \n",
    "    df_h = df.apply(lambda x:x.max() <= ch,  axis = 1 )\n",
    "    df_l = df.apply(lambda x:x.min() >= cl,  axis = 1 )\n",
    "    \n",
    "    df['dfh'] = df_h\n",
    "    df['dfl'] = df_l\n",
    "    df['code'] =code\n",
    "#     out=df.iloc[-1].apply(lambda x: True if x>cl and x < ch else False)\n",
    "    df=df.reset_index('tradeDate')\n",
    "    df=df.set_index(['code','tradeDate'])\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bbb=select1('sh000001',data_df.loc['sh000001',])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bbb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bbb=bbb.set_index(['code','tradeDate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=bbb.set_index(['code','tradeDate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output=None\n",
    "for code in codes:\n",
    "  aaa=data_df.loc[code,]\n",
    "  out=select1(code, aaa)\n",
    "  if output is None:\n",
    "    output = out\n",
    "  else:\n",
    "#     print(code)\n",
    "    output=output.append(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output.query('dfh==True and dfl==True').to_csv('out1.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bb=select1('000001',aaa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(bb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import talib as tdx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aaa=pd.read_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aaa.set_index('vol').sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=readTdxLdayFile()\n",
    "df['mon'] = df.tradeDate.apply(lambda x : str(x)[0:6])\n",
    "df=df.set_index(['tradeDate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfmax=df.groupby(['mon']).apply(lambda x: x[x.high ==x.high.max()])\n",
    "dfmax.drop_duplicates(subset=['high','mon'],keep='first',inplace=True)\n",
    "dfmin=df.groupby(['mon']).apply(lambda x: x[x.low ==x.low.min()])\n",
    "dfmin.drop_duplicates(subset=['low','mon'],keep='first',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfmax.to_csv('max.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfmin.to_csv('min.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for x in dfmax.index:\n",
    "  print(df.loc[x[1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
